package com.yourname.myapplication.ui.viewmodel

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.yourname.myapplication.App
import com.yourname.myapplication.data.model.Question
import com.yourname.myapplication.data.room.database.AppDatabase
import kotlinx.coroutines.launch

class ChapterPracticeViewModel(private val chapter: Int) : ViewModel() {
    private val questionDao = AppDatabase.getInstance(App.instance).questionDao()
    var currentQuestionIndex = 0
    var questions: List<Question> = emptyList()

    init {
        viewModelScope.launch {
            questionDao.getQuestionsByChapter(chapter).collect { allQuestions ->
                questions = allQuestions
            }
        }
    }

    fun getCurrentQuestion() = questions.getOrNull(currentQuestionIndex)

    fun nextQuestion() {
        currentQuestionIndex++
    }
}